/*
 * $Id: StatusCommand.java 9 2008-01-24 17:35:25Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.syncml.protocol;

import org.devtcg.syncml.model.DatabaseMapping;

import org.w3c.dom.*;

public class StatusCommand extends BaseCommand
{
	protected long mCmdId;
	protected long mMsgId;
	protected String mCmd;
	protected String mSourceRef;
	protected String mTargetRef;
	protected int mStatus;

	public StatusCommand(Element command)
	{
		super("Status");

		NodeList children = command.getChildNodes();

		for (int i = 0; i < children.getLength(); i++)
		{
			Node n = children.item(i);

			if (n.getNodeName().equals("MsgRef") == true)
				mMsgId = Long.valueOf(((Text)n.getFirstChild()).getNodeValue());
			else if (n.getNodeName().equals("CmdRef") == true)
			 	mCmdId = Long.valueOf(((Text)n.getFirstChild()).getNodeValue());
			else if (n.getNodeName().equals("Cmd") == true)
			 	mCmd = ((Text)n.getFirstChild()).getNodeValue();
			else if (n.getNodeName().equals("SourceRef") == true)
			 	mSourceRef = ((Text)n.getFirstChild()).getNodeValue();
			else if (n.getNodeName().equals("TargetRef") == true)
			 	mTargetRef = ((Text)n.getFirstChild()).getNodeValue();
			else if (n.getNodeName().equals("Data") == true)
				mStatus = Integer.valueOf(((Text)n.getFirstChild()).getNodeValue());
			else if (n.getNodeName().equals("CmdID") == true)
				mID = Long.valueOf(((Text)n.getFirstChild()).getNodeValue());
		}
	}

	public StatusCommand(long msgId, long cmdId, String cmd)
	{
		super("Status");

		mMsgId = msgId;
		mCmdId = cmdId;
		mCmd = cmd;
	}

	public long getMsgId()
	{
		return mMsgId;
	}

	public long getCmdId()
	{
		return mCmdId;
	}

	public String getCmd()
	{
		return mCmd;
	}

	public void setSourceRef(String ref)
	{
		mSourceRef = ref;
	}

	public void setTargetRef(String ref)
	{
		mTargetRef = ref;
	}

	public int getStatus()
	{
		return mStatus;
	}

	public void setStatus(int status)
	{
		mStatus = status;
	}

	public String toString()
	{
		StringBuilder b = new StringBuilder();

		b.append("<Status>\n");
		b.append("<CmdID>").append(getId()).append("</CmdID>\n");
		b.append("<MsgRef>").append(mMsgId).append("</MsgRef>\n");
		b.append("<CmdRef>").append(mCmdId).append("</CmdRef>\n");
		b.append("<Cmd>").append(mCmd).append("</Cmd>\n");
		b.append("<SourceRef>").append(mSourceRef).append("</SourceRef>\n");
		b.append("<TargetRef>").append(mTargetRef).append("</TargetRef>\n");
		b.append("<Data>").append(mStatus).append("</Data>\n");
		
		String raw = getRaw();

		if (raw != null)
			b.append(raw);

		b.append("</Status>\n");

		return b.toString();
	}
}
